<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: magic_sofa.php 6894 2010-03-26 09:34:23Z wangjinbo $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(submitcheck('usesubmit')) {

	if(empty($_G['tid'])) {
		showmessage('magics_info_nonexistence');
	}

	$post = getpostinfo($_G['tid'], 'tid', array('fid', 'dateline'));
	checkmagicperm($magicperm['forum'], $post['fid']);

	$firstsofa = DB::result_first("SELECT COUNT(*) FROM ".DB::table('forum_threadmod')." WHERE magicid='$magicid' AND tid='$_G[tid]'");

	if($firstsofa >= 1) {
		showmessage('magics_SOFA_message', '', array(), array('login' => 1));
	}

	$sofamessage = $lang['SOFA_message'];
	$dateline = $post['dateline'] + 1;

	insertpost(array(
		'fid' => $post['fid'],
		'tid' => $_G['tid'],
		'first' => '0',
		'author' => $_G['username'],
		'authorid' => $_G['uid'],
		'dateline' => $dateline,
		'message' => $sofamessage,
		'useip' => $_G['clientip'],
		'usesig' => '1',
	));

	DB::query("UPDATE ".DB::table('forum_thread')." SET replies=replies+1, moderated='1' WHERE tid='$_G[tid]'", 'UNBUFFERED');
	DB::query("UPDATE ".DB::table('forum_forum')." SET posts=posts+1, todayposts=todayposts+1 WHERE fid='$post[fid]'", 'UNBUFFERED');

	usemagic($magicid, $magic['num']);
	updatemagiclog($magicid, '2', '1', '0', $_G['tid']);
	updatemagicthreadlog($_G['tid'], $magicid, $magic['identifier'], $expiration);

	if($thread['authorid'] != $_G['uid']) {
		sendpm($thread['authorid'], 'magics_use_subject', 'magic_thread', 0);
	}

	showmessage('magics_operation_succeed', '', array('succeed' => 1));

}

function showmagic() {
	global $_G, $lang;
	magicshowtype($lang['option'], 'top');
	magicshowsetting($lang['target_tid'], 'tid', $_G['tid'], 'hidden');
	magicshowtype('', 'bottom');
}

?>